package com.global.gbinventory.barang.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import com.global.gbinventory.barang.tablemodel.GrupList;
import com.global.gbinventory.barang.tablemodel.SubgrupTableModel;
import com.global.gbinventory.barang.util.CONSTANT;
import com.global.gbinventory.barang.view.GrupFrame;
import com.global.gbinventory.main.controller.MainController;

public class GrupController {

	private MainController main;
	private GrupFrame frame;
	private GrupList grupList;
	
	private boolean isGrup, isNewGrup;
	private String oldGrupNama;
	
	
	public GrupController(MainController main){
		this.main = main;
		this.grupList = new GrupList(main.getDBMaster());
	}
	
	public void start(boolean viewedOnly){
		initializeFrame(viewedOnly);
		main.addFrameToDesktopPane(frame);
		frame.moveToFront();
	}
	
	private void initializeFrame(boolean viewedOnly){
		frame = new GrupFrame();
		frame.setChooseGrupModel(getGrupComboBoxModel());
		frame.setChosenGrupIndex(0);
		
		int index = frame.getChosenGrupIndex();
		SubgrupTableModel tmSubgrup = grupList.getSubgrupTableModel(index);
		frame.setSubgrupTableModel(tmSubgrup);
		
		frame.setEnabledButtonSave(false);
		
		Object obj = frame.getChosenGrupObject();
		frame.setTxtFldChosenGrup(obj.toString());
		frame.setLblGrupTable(obj.toString());
		
		frame.displayFrame(true, viewedOnly);
		
		isGrup = false;
		isNewGrup = false;
		oldGrupNama = "";
		addListeners();
	}
	
	private DefaultComboBoxModel getGrupComboBoxModel(){
		return new DefaultComboBoxModel(grupList.getAllGrupNama());
	}
	
	private void addListeners(){
		frame.addBtnNewListener(new BtnNewListener());
		frame.addBtnEditListener(new BtnEditListener());
		frame.addBtnDeleteListener(new BtnDeleteListener());
		frame.addBtnSaveListener(new BtnSaveListener());
		frame.addBtnCancelListener(new BtnCancelListener());
		
		frame.addcmbBoxChooseGrupListener(new CmbBoxChooseGroupListener());
		
		frame.addSubgrupTableListener(new TableSubgrupListener());
		frame.addSubgrupTableKeyListener(new EnterKeyListener());
		
		frame.addBtnOkListListener(new BtnOkListListener());
		frame.addBtnCancelListListener(new BtnCancelListListener());
		
	}
	
	class BtnNewListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
			frame.setChooseGrupModel(new DefaultComboBoxModel());
			frame.setTxtFldChosenGrup("");
			frame.setEditableTxtFldChosenGrup(true);
			frame.setLblGrupTable("");
			frame.setEnabledTableSubgrup(false);
			frame.requestFocusTxtFldChosenGrup();
			
			isNewGrup = true;
			isGrup = true;
			
			activateSave();
	
		}
		
	}
	
	class BtnEditListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			oldGrupNama = frame.getChosenGrup();
			isNewGrup = false;
			isGrup = true;
			
			frame.setEditableTxtFldChosenGrup(true);
			frame.setEnabledTableSubgrup(false);

			frame.requestFocusTxtFldChosenGrup();
			
			activateSave();
		}
		
	}
	
	class BtnDeleteListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			int index = frame.getChosenGrupIndex();

			String title = "Konfirmasi Penghapusan";
			String message = "Anda yakin mau menghapus \"" + frame.getChosenGrup() + " \" ?\n" +
					"Semua subgrup dari grup ini juga akan dihapus !!!";
			int ans = JOptionPane.showConfirmDialog(frame, message, title, JOptionPane.YES_NO_OPTION);
			
			if(ans == JOptionPane.YES_OPTION){
				grupList.removeGrup(index);
				cancel();
			}

		}
		
	}
	
	class BtnSaveListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {	
			
			String newGrupNama = frame.getChosenGrup();
			int index = frame.getChosenGrupIndex();
			
			if(newGrupNama.equals("")){
				String title = "Save Data Gagal";
				String message = "Kolom grup barang mesti diisi !!!";
				JOptionPane.showMessageDialog(frame, message, title, JOptionPane.INFORMATION_MESSAGE);
			}else{
				
				if(isGrup){
					
					if(isNewGrup){
						if(grupList.isExistingGrupNama(newGrupNama)){
							
							String title = "Save Data Gagal";
							String message = "Nama grup sudah dipakai. Silakan masukkan nama yang lain !!!";
							JOptionPane.showMessageDialog(frame, message, title, JOptionPane.INFORMATION_MESSAGE);
							
							return;
						}else{
							grupList.addNewGrup(newGrupNama);
						}
						
					}else{
						if(newGrupNama.equalsIgnoreCase(oldGrupNama)){
							grupList.updateGrup(index, newGrupNama);
						} else if(grupList.isExistingGrupNama(newGrupNama)){
							String title = "Save Data Gagal";
							String message = "Nama grup sudah dipakai. Silakan masukkan nama yang lain !!!";
							JOptionPane.showMessageDialog(frame, message, title, JOptionPane.INFORMATION_MESSAGE);
							
							return;	
						} else{
							grupList.updateGrup(index, newGrupNama);
						}
						
					}
					
				} else{
					SubgrupTableModel tmSubgrup = grupList.getSubgrupTableModel(index);
					tmSubgrup.save();
				}
			
				String title = "Save Group Barang";
				String message = "Save group barang berhasil";
				JOptionPane.showMessageDialog(frame, message, title, JOptionPane.INFORMATION_MESSAGE);
				
				cancel();
			}
				
		}
		
	}
	
	class BtnCancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			cancel();
		}
		
	}
	
	class CmbBoxChooseGroupListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			JComboBox cb = (JComboBox) e.getSource();
			int index = cb.getSelectedIndex();
			String grup = (String) cb.getSelectedItem();
			frame.setTxtFldChosenGrup(grup);
			frame.setLblGrupTable(grup);
			
			frame.setSubgrupTableModel(grupList.getSubgrupTableModel(index));
			frame.addSubgrupTableListener(new TableSubgrupListener());

		}
		
	}
	
	class EnterKeyListener implements KeyListener{

		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub	
		}

		@Override
		public void keyPressed(KeyEvent e) {
			if(e.getKeyCode() == KeyEvent.VK_RIGHT){
				int index = frame.getChosenGrupIndex();
				SubgrupTableModel tmSubgrup = grupList.getSubgrupTableModel(index);
				int lastRow = tmSubgrup.getLastRow();
				isGrup = false;
				activateSave();
				Object obj = null;
				
				if(lastRow > -1){
					obj = tmSubgrup.getValueAt(lastRow, CONSTANT.SUBGRUP_TM_NAMA);
					if(!(obj.toString().equals(""))){
						tmSubgrup.addNewSubgrup(new Object[]{""});
						frame.getSubgrupTable().changeSelection(lastRow + 1, CONSTANT.SUBGRUP_TM_NAMA, false, false);
						frame.getSubgrupTable().editCellAt(lastRow + 1, CONSTANT.SUBGRUP_TM_NAMA);
						frame.getSubgrupTable().getEditorComponent().requestFocusInWindow();
					}else{
						String title = "Penambahan Kolom Subgrup Gagal";
						String message = "Silakan isi kolom subgrup yang kosong terlebih dahulu !!!";
						JOptionPane.showMessageDialog(frame, message, title, JOptionPane.INFORMATION_MESSAGE);
					}		
				}else{
					tmSubgrup.addNewSubgrup(new Object[]{""});
					frame.getSubgrupTable().changeSelection(lastRow + 1, CONSTANT.SUBGRUP_TM_NAMA, false, false);
					frame.getSubgrupTable().editCellAt(lastRow + 1, CONSTANT.SUBGRUP_TM_NAMA);
					frame.getSubgrupTable().getEditorComponent().requestFocusInWindow();
				}
			}
		}

		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
		}
		
	}

	class TableSubgrupListener implements TableModelListener{

		@Override
		public void tableChanged(TableModelEvent e) {
			int firstRow = e.getFirstRow();
			
			switch (e.getType()) {
			case TableModelEvent.INSERT:
				break;
			case TableModelEvent.DELETE :
				break;
			case TableModelEvent.UPDATE:
				int index = frame.getChosenGrupIndex();
				SubgrupTableModel tmSubgrup = grupList.getSubgrupTableModel(index);
				tmSubgrup.updateEditedRow(firstRow);
				activateSave();
				isGrup = false;
				
				break;
			default:
				break;
			}
			
		}
		
	}
	
	class BtnOkListListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			String grup = frame.getChosenGrup();
			int selectedGrupIndex = frame.getChosenGrupIndex();
			int selectedSubgrupIndex = frame.getSubgrupTableSelectedRow();
			String subgrup = grupList.getSubgrupTableModel(selectedGrupIndex).getSubgrup(selectedSubgrupIndex).getNama();
			
			main.getBarangController().fillGrupData(grup, subgrup);
		
			frame.disposeFrame();
			main.removeFrameFromDesktopPane(frame);
		}
		
	}
	
	class BtnCancelListListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			frame.disposeFrame();
			main.removeFrameFromDesktopPane(frame);
		}
		
	}
	
	private void cancel(){
		if(!isGrup){
			int index = frame.getChosenGrupIndex();
			SubgrupTableModel tmSubgrup = grupList.getSubgrupTableModel(index);
			tmSubgrup.cancel();
		}
		
		frame.setEnabledButtonNew(true);
		frame.setEnabledButtonEdit(true);
		frame.setEnabledButtonDelete(true);
		frame.setEnabledButtonSave(false);
		frame.setEnabledButtonCancel(false);
		frame.setEnabledTableSubgrup(true);

		frame.setEditableTxtFldChosenGrup(false);
		frame.setEnabledCmbBoxChooseGrup(true);		
		
		frame.setChooseGrupModel(getGrupComboBoxModel());
		frame.setChosenGrupIndex(0);
		
		isGrup = false;
		isNewGrup = false;
		oldGrupNama = "";
	}
	
	private void activateSave(){
		frame.setEnabledButtonNew(false);
		frame.setEnabledButtonEdit(false);
		frame.setEnabledButtonDelete(false);
		frame.setEnabledButtonSave(true);
		frame.setEnabledButtonCancel(true);
		
		frame.setEnabledCmbBoxChooseGrup(false);
	}
	
}
